<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>订单列表</title>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/themes/default/easyui.css"/>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/themes/icon.css"/>
	<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/plugins/jQuery/demo.css"/>
	<script src="${pageContext.request.contextPath }/plugins/jQuery/jquery.min.js"></script>
	<script src="${pageContext.request.contextPath }/plugins/jQuery/jquery.easyui.min.js"></script>
	<script src="${pageContext.request.contextPath}/plugins/jQuery/easyui-lang-zh_CN.js"></script>
	<script src="${pageContext.request.contextPath }/admin/js/easyui_mergecell.js"></script>
	<script type="text/javascript">
		$(function(){
		    
		     $('#station').combobox({
				valueField:'station_id', //值字段
				textField:'station_name', //显示的字段
				url:'${pageContext.request.contextPath }/admin/station/findStationByKind/1',
				onLoadSuccess : function(data) {
		            $('#station').combobox("setValue",-1);
	        	}
	        });
	        
	        /*
	        $('#orderstaus').combobox({
				valueField:'dict_code',
				textField:'dict_name',
				url:'${pageContext.request.contextPath }/order/findorderstatus'
				
			});
			$('#orderstaus').combobox("setValue","-1");
			*/
			$('#orderstaus').combobox({
				valueField:'value',
				textField:'text',
				data:[{"value":"-1","text":"--- 全部 ---"},
					{"value":"no_pay","text":"待付款"},
					{"value":"nosend","text":"待发货"},
					{"value":"no_pick","text":"待提货"},
					{"value":"finsh","text":"已完成"},
					{"value":"over","text":"订单关闭"},
					{"value":"0","text":"有效订单[扣除关闭订单]"}
				]
			});
			$('#orderstaus').combobox("setValue","0");
		  
			loadTable();
			$('#dg').dialog('close');
			
		});
		
		function loadTable(){
		    
		     //设置时间  
			var curr_time = new Date();     
		   var s = $("#start_time").textbox('getValue');
		   var e = $("#end_time").textbox('getValue');
		   
		   if(s != null && s != '' && e !=null && e!='')
		   {
		       $("#start_time").textbox("setValue",s);
		       $("#end_time").textbox("setValue",e);
		   }else{
		       $("#start_time").textbox("setValue",myformatter(curr_time)); 
		       $("#end_time").textbox("setValue",myformatter(curr_time));
		   }
			var sortFlag = false;
			$('#dg').datagrid({
				rownumbers:true,
				singleSelect:true,
				pagination:false,
		 		url:'${pageContext.request.contextPath }/order/financeorderlist',
		 		queryParams:{
		 			 stationid:$('#station').combobox("getValue"),
		 			 startDate:$("#start_time").textbox('getValue'),
		 			 endDate:$("#end_time").textbox('getValue'),
		 			 orderStatus:$('#orderstaus').combobox("getValue"),
		 		},
		 		rowStyler: function(index,row){
					if (row.goods_name=="合计" || row.goods_name=="汇总统计"){
		//				return 'background-color:#6293BB;color:#fff;font-weight:bold;';
						return 'font-weight:bold;';
					}
				},
	    		columns:[[
					{field:'order_date',title:'日期',align:'center',width:100},
					{field:'station',title:'站点',align:'center',width:120},
					{field:'send_time',title:'需到货时间',align:'center',width:180},
					{field:'goods_name',title:'品名',align:'center',width:200},
					{field:'assign_num',title:'应发货数(件)',align:'center',width:80},
					{field:'sale_price',title:'单价(元)',align:'center',width:60},
					{field:'ammounts',title:'金额(元)',align:'right',width:80},
					{field:'sale_num',title:'实际订单数',align:'center',width:100}				
	 			]],
				onLoadSuccess:function(data){
 					if(!sortFlag) $(this).datagrid("autoMergeCells",['station','order_date','send_time']);
				}
			});
		}
		
		 //指定列求和
        function compute(colName) {
            var rows = $('#dg').datagrid('getRows');
            var total = 0;
            for (var i = 0; i < rows.length; i++) {
                total += parseFloat(rows[i][colName]);
            }
            return total;
        }
		
		function showImg(imgUrl){
			$('#dlg').dialog('open');
			var image=document.getElementById("stationImg");
			var src="${pageContext.request.contextPath }"+imgUrl;
			image.setAttribute("src",src );
		}
		
		// 关闭订单
		function close_order(orderid,status){
		     var os="";
		    if(status=='nosend'){
		    	os="订单[编号："+orderid+"]处于[待发货]状态，您确认要关闭该订单吗？<br/><span style='color:red;'>[温馨提示：]一旦关闭订单，将不可恢复！";
		    }else if(status=='no_pay'){
		    	os="订单[编号："+orderid+"]处于[待付款]状态，您确认要关闭该订单吗？<br/><span style='color:red;'>[温馨提示：]一旦关闭订单，将不可恢复！";
		    }
		   $.messager.confirm('Confirm', os, function(r){
				if (r){
					$.post("${pageContext.request.contextPath}/order/changeStatusToOver",{orderid:orderid},
						function(data){
							if(data=="0"){
								$.messager.alert('系统消息','<span style=\"color:red;\">订单关闭失败，请稍后再试！</span>');	 
							}else{
								$.messager.alert('系统消息','订单关闭成功！');
								$('#dg').datagrid('reload'); 
							}
								
						},"text");
				}else{
					$.messager.alert('系统消息','订单关闭操作取消！');
				}
			}); 
		}
		
		
		// 查看订单信息
		function findOrder(orderid,orderdate){
			var od=orderdate.substr(0,10);
			var str=od.replace(new RegExp("-","gm"),"@");
			show("${pageContext.request.contextPath}/order/findOrderDetails/"+orderid+"-"+str,"订单管理");
		}
		
		// 修改订单状态
		function changeOrderStatus(orderid,nosend){
			$.messager.confirm('Confirm', "您确认要更改订单[编号："+orderid+"]的状态吗？<br/><span style='color:red;'>[温馨提示：]一旦更改，将不可撤销！", function(r){
				if (r){
					$.post("${pageContext.request.contextPath}/order/changestatus",{orderid:orderid,status:nosend},
					function(data){
						if(data=="0"){
							$.messager.alert('系统消息','<span style=\"color:red;\">订单变更失败，请稍后再试！</span>');	 
						}else{
							$.messager.alert('系统消息','订单变更成功！');
							$('#dg').datagrid('reload'); 
						}
							
					},"text");
				}else{
					$.messager.alert('系统消息','订单关闭操作取消！');
				}
			}); 
			
		}
		
	 function myformatter(date){  
	      var y = date.getFullYear();  
	      var m = date.getMonth()+1;  
	      var d = date.getDate();  
	      return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);  
	   }  
	          
	function myparser(s){  
	    if (!s) return new Date();  
	    var ss = (s.split('-'));  
	    var y = parseInt(ss[0],10);  
	    var m = parseInt(ss[1],10);  
	    var d = parseInt(ss[2],10);  
	    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){  
	        return new Date(y,m-1,d);  
	    } else {  
	        return new Date();  
	    }  
	} 	

		// 打开一个新的选项卡，并制定选项卡的名称
		function show(url,name){
			parent.window.addTab(name,url);
		}
		
		function findData(){
			loadTable();
		}
		
		// 正则表达式JS实现替换所有的函数
		String.prototype.replaceAll = function(s1,s2){
			return this.replace(new RegExp(s1,"gm"),s2);
		}
		
	
		function toExcel(){
			$('#ff').submit();
		}
	</script>
	<style type="text/css">
		body{
		font-family:'微软雅黑';}
	</style>
  </head>
  
  <body>
    <table id="dg" title="订单列表" toolbar="#searchtool" loadMsg="正在查询..." style="width:90%;height:700px;"></table>
    <div id="searchtool" style="padding:5px;height:auto;">
    	<form action="${pageContext.request.contextPath}/order/toFinanceListExcel" method="post" id="ff">  
	       <span style="margin:0px 12px;">站点:</span><input id="station" name="stationid" class="easyui-textbox" style="width:150px;height:26px"/> 
	       <span style="margin:0px 12px;">下单时间:</span><input id="start_time" name="startDate" class="easyui-datebox" data-options="formatter:myformatter,parser:myparser" style="width:100px" value="${today }"> -- <input id="end_time" name="endDate" class="easyui-datebox" data-options="formatter:myformatter,parser:myparser" style="width:100px"  value="${today }">
	       <span style="margin:0px 12px;">订单状态:</span><input id="orderstaus" name="orderStatus" class="easyui-textbox" style="width:180px;height:26px"/> 
	       <a href="javascript:findData()" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width:80px;margin:0px 3px;">查询</a>
	       <a href="javascript:findData()" class="easyui-linkbutton" data-options="iconCls:'icon-reload'" style="width:80px;margin:0px 3px;">刷新</a>
	       <c:if test="${LOGIN_USER.ROLE ne 8 }">
	       <a href="javascript:toExcel()" class="easyui-linkbutton" data-options="iconCls:'icon-my-save'" style="width:130px;margin:0px 3px;">导出Excel</a>
	       </c:if>
       	</form>
    </div>
  </body>
</html>
